package com.yc.web.servlets;

import com.yc.bean.Books;
import com.yc.bean.CartItem;
import com.yc.model.JsonModel;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwei
 * @create 2024-05-13 21:25
 */
@WebServlet("/books.action")
public class BooksServlet extends BaseServlet{

    protected void getCartItem(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session=req.getSession();  //取出session回话
        Map<Integer,CartItem> cart=new HashMap<Integer,CartItem>();  //购物车
        if (session.getAttribute("cart")!=null){
            cart= (Map<Integer, CartItem>) session.getAttribute("cart");
        }
        JsonModel jm=new JsonModel();
        jm.setCode(1);
        jm.setObj(cart.values());
        super.writeJson(jm,resp);
    }


    //移除购物车选项
    protected void remove(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id= Integer.parseInt(req.getParameter("id"));
        HttpSession session=req.getSession();   //取出session对话
        Map<Integer,CartItem>cart =new HashMap<Integer,CartItem>();//购物车集合
        if (session.getAttribute("cart")!=null){
            cart= (Map<Integer, CartItem>) session.getAttribute("cart");
        }
        if (cart.containsKey(id)){
            cart.remove(id);
        }
        session.setAttribute("cart",cart);
        JsonModel jm=new JsonModel();
        jm.setCode(1);
        jm.setObj(cart.values());
        super.writeJson(jm,resp);
    }
    //加入购物车
    protected void addCart(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
        int id= Integer.parseInt(req.getParameter("id"));
        int num= Integer.parseInt(req.getParameter("num"));
        HttpSession session=req.getSession();//取出session会话对象
        Map<Integer, CartItem> cart=new HashMap<Integer,CartItem>();  //购物车集合
        if (session.getAttribute("cart")!=null){
            cart=(Map<Integer,CartItem>)session.getAttribute("cart");
        }
        CartItem ci=null;
        if (cart.containsKey(id)){
            //判断  用户是否已经购买了这个商品
            ci=cart.get(id);
            //如果有
            ci.setNum(ci.getNum()+num);
        }else {
            //查出这个商品对象  构建一个CartItem对象
            //1.根据id查这本书
            List<Books>list=dbHelper.select(Books.class,"select*from books where id=?",id);
            Books b=null;
            if (list!=null&&list.size()>0){
                b=list.get(0);
            }
            //2.创建购物条目  设置数量
            ci=new CartItem();
            ci.setBook(b);
            ci.setNum(num);
        }
        //判断购物车的数量是否<=0  如果是  则从cart中remove掉这个购物条目
        //3.将购物条目放到Map<Integer,CartItem> cart购物车
        //不是  则将购物条目添加到Cart中去
        if (ci.getNum()<=0){
            cart.remove(id);
        }else {
            ci.getSmallCount();  //强制计算小计
            cart.put(id,ci);
        }
        //4.存入session
        session.setAttribute("cart",cart);
        //5.返回jsonModel(code=1，obj=cart.value)
        JsonModel jm=new JsonModel();
        jm.setCode(1);
        jm.setObj(cart.values());
        super.writeJson(jm,resp);
    }
    //获取数据库中的所有书
    protected void getAllBooks(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
        JsonModel jm=new JsonModel();
        List<Books>list=dbHelper.select(Books.class,"select*from books");
        //因为BassServlet中已经写好了异常捕获的代码    所以这里方法中的异常直接抛出
        if (list.size()>=0){
            jm.setCode(1);
            jm.setObj(list);
            System.out.println(list);
        }else {
            jm.setCode(0);
            jm.setError("数据查询为空");
        }
        super.writeJson(jm,resp);
    }
}
